package com.zjxf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.UmsAdmin;
import com.zjxf.base.bean.po.UmsStore;
import com.zjxf.base.bean.ro.RoUmsStore;
import com.zjxf.base.bean.vo.VoUmsStore;
import com.zjxf.base.common.OperationException;
import com.zjxf.base.common.SysConst;
import com.zjxf.repository.UmsAdminRepository;
import com.zjxf.repository.UmsStoreRepository;
import com.zjxf.service.UmsStoreSellerService;
import com.zjxf.service.UmsStoreService;
import com.zjxf.utils.*;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 商铺
 *
 * @author ZhangWei
 * @date 2021/3/27/027 16:02
 */
@Service
@AllArgsConstructor
@Transactional(rollbackOn = RuntimeException.class)
public class UmsStoreServiceImpl implements UmsStoreService {

    private final UmsStoreRepository umsStoreRepository;

    private final UmsAdminRepository umsAdminRepository;

    private final UmsStoreSellerService umsStoreSellerService;

    public Optional<UmsStore> findById(Long id) {
        return umsStoreRepository.findByIdAndDeleteState(id, SysConst.DeleteState.UN_DELETE_STATE.getCode());
    }

    public Optional<UmsStore> findByName(String name) {
        return umsStoreRepository.findByNameAndDeleteState(name, SysConst.DeleteState.UN_DELETE_STATE.getCode());
    }

    public Optional<UmsAdmin> findAdminById(Long id) {
        return umsAdminRepository.findByIdAndDeleteState(id, SysConst.DeleteState.UN_DELETE_STATE.getCode());
    }

    @Override
    public RadomUP.UP add(VoUmsStore.addOrUpdate vo, UmsAdmin umsAdmin) throws OperationException {
        Optional<UmsStore> optionalByName = this.findByName(vo.getName());
        if (optionalByName.isPresent()) {
            throw new OperationException("店铺名称已存在，换一个试试");
        } else {
            UmsStore umsStore = Conversion.voTurnPo(vo, new UmsStore());
            UmsStore umsStoreSave = umsStoreRepository.save(umsStore);
            return umsStoreSellerService.defaultSeller(umsStoreSave.getId());
        }
    }

    @Override
    public void Update(VoUmsStore.addOrUpdate vo, UmsAdmin umsAdmin) throws OperationException {
        UmsStore umsStore = this.findById(vo.getId()).orElseThrow(() -> new OperationException(SysConst.DATA_NOT_FOND));
        umsStore = Conversion.voTurnPo(vo, umsStore);
        umsStoreRepository.save(umsStore);
    }

    @Override
    public void delete(Long id) throws OperationException {
        UmsStore umsStore = this.findById(id).orElseThrow(() -> new OperationException(SysConst.DATA_NOT_FOND));
        umsStore.setDeleteState(SysConst.DeleteState.DELETE_STATE.getCode());
        umsStoreRepository.save(umsStore);
    }

    @Override
    public Page<UmsStore> page(VoUmsStore.filter filter) {
        List<SearchFilter> searchFilters = new ArrayList<>();
        if (StringUtils.isNotBlank(filter.getKeyWords())) {
            searchFilters.add(new SearchFilter("name", SearchFilter.Operator.LIKE, filter.getKeyWords()));
        }
        Specification<UmsStore> specification = DynamicSpecifications.bySearchFilterByBasic(searchFilters, filter.getStartTime(), filter.getEndTime());
        return umsStoreRepository.findAll(specification, PageUtils.buildPageRequest(filter.getPageNum(), filter.getPageSize()));
    }

    @Override
    public JSONObject pageRo(Page<UmsStore> umsStorePage) {
        List<UmsStore> pageList = umsStorePage.getContent();
        List<RoUmsStore.AdminPage> roList = pageList.stream().map(item -> {
            RoUmsStore.AdminPage roUmsStore = Conversion.poTurnRo(item, new RoUmsStore.AdminPage());
            return roUmsStore;
        }).collect(Collectors.toList());
        return PageUtils.page(umsStorePage, roList);
    }

    @Override
    public UmsStore getEntity(Long id) throws OperationException {
        return this.findById(id).orElseThrow(() -> new OperationException(SysConst.DATA_NOT_FOND));
    }

    @Override
    public RoUmsStore.Details getEntityRo(UmsStore umsStore) {
        RoUmsStore.Details details = Conversion.poTurnRo(umsStore, new RoUmsStore.Details());
        return details;
    }

    @Override
    public List<UmsStore> findAll() {
        Specification<UmsStore> specification = DynamicSpecifications.bySearchFilterByBasic();
        return umsStoreRepository.findAll(specification);
    }

}
